home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13147 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  75 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Philippe Verdy <100105.3120@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Is this possible?
  5. Date: 24 Mar 1996 01:01:26 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4j26t6$shm@arl-news-svc-3.compuserve.com>
  8. NNTP-Posting-Host: ad53-232.compuserve.com
  9.  
  10. Sensarn <txs53132@bayou.uh.edu> s'Θcrit :
  11. > Is it possible to use NEW for FAR memory allocation in the SMALL 
  12. > memory model?  I get NULL POINTER ASSIGNMENT when I try this using 
  13. > farmalloc() for my FAR allocation:
  14. > unsigned char **ptr; /* Pointer to unsigned char pointers */
  15. > ptr=(unsigned char far **)farmalloc(1); /* Create a pointer */
  16. > ptr[0]=(unsigned char far *)farmalloc(64001); /* Allocate memory */
  17. > I want to create an array of pointers -- each pointing to 64016 bytes -- 
  18. > I get NULL POINTER ASSIGNMENT instead.  I tried this:
  19. > unsigned char far **ptr;
  20. > ptr=new unsigned char far *[1];
  21. > ptr[0]=new unsigned char far[64001];
  22. > I got no errors -- except for the fact that I didn't have enough NEAR 
  23. > heap for the allocation.  Please try to help me,
  24. > -- 
  25. > ______________________________
  26. > Steven Sensarn
  27. > E-Mail - txs53132@bayou.uh.edu
  28. > ______________________________
  29.  
  30. When you call : (new T), the result is a (T*) and not a
  31. (T far *). There is no support for a _farnew operator.
  32.  
  33. You can obtain such a result using the placement arguments
  34. for the new operator, or using class specific implementation
  35. eg:
  36.  
  37. class farheap {
  38. public:
  39.  static void far * operator new(size_t sz) throw xalloc;
  40.  static operator delete(void far *);
  41. };
  42.  
  43. void far * farheap::operator new(size_t sz) throw xalloc
  44. {
  45.    void far *p = farmalloc(sz);
  46.    if (!p) throw xalloc(sz);
  47.    return p;
  48. }
  49.  
  50. void farheap::operator delete(void far *p)
  51. {
  52.   if (p) farfree(p);
  53. }
  54.  
  55. Then derive your far-allocated classes from this class, or use
  56. farheap::new and farheap::delete explicitly:
  57.  
  58.   char far *p = farheap::new char far[1000];
  59.   ...
  60.   farheap::delete p;
  61.  
  62. As an alternative, you can also use the placement argument to
  63. build specific memory allocation algorithms using
  64. memory pool classes of your own (for example using the Windows
  65. Global memory pool, or the DDE shared memory pool, or a
  66. file mapped in memory allocation used as a fast private
  67. swap area)... The mechanism of constructors will still apply
  68. while using different memory allocation schemes for objects!
  69.